package com.zyf.chapter02.creation.builder;

import com.zyf.chapter01.principle.DRY.utils.StringUtils;

/**
 * 当配置项增多时，就不能用构造函数了，会导致可读性查、参数可能传错的问题
 * 使用 set 方法
 * @author yifeng
 */
public class V2ResourcePoolConfig {
    private static final int DEFAULT_MAX_TOTAL = 8;
    private static final int DEFAULT_MAX_IDLE = 8;
    private static final int DEFAULT_MIN_IDLE = 0;

    /**
     * 必填
     * 默认值：没有
     */
    private String name;
    /**
     * 非必填
     * 默认值 8
     */
    private int maxTotal = DEFAULT_MAX_TOTAL;
    /**
     * 非必填
     * 默认值 8
     */
    private int maxIdle = DEFAULT_MAX_IDLE;
    /**
     * 非必填
     * 默认值 0
     */
    private int minIdle = DEFAULT_MIN_IDLE;

    public V2ResourcePoolConfig(String name) {
        if (StringUtils.isBlank(name)) {
            throw new IllegalArgumentException("name should not be empty.");
        }
        this.name = name;
    }

    public void setMaxTotal(Integer maxTotal) {
        if (maxTotal != null) {
            if (maxTotal <= 0) {
                throw new IllegalArgumentException("maxTotal should be positive.");
            }
            this.maxTotal = maxTotal;
        }
    }

    public void setMaxIdle(Integer maxIdle) {
        if (maxIdle != null) {
            if (maxIdle <= 0) {
                throw new IllegalArgumentException("maxIdle should not be negative.");
            }
            this.maxIdle = maxIdle;
        }
    }

    public void setMinIdle(Integer minIdle) {
        if (minIdle != null) {
            if (minIdle <= 0) {
                throw new IllegalArgumentException("minIdle should not be negative.");
            }
            this.minIdle = minIdle;
        }
    }

    public static void main(String[] args) {
        V2ResourcePoolConfig config = new V2ResourcePoolConfig("dbconnectionpool");
        config.setMaxTotal(16);
        config.setMinIdle(8);
    }
}
